package com.dxysun.springboot.view;

import com.dxysun.springboot.convert.MyCustomHttpMessageConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class MyMappingJackson2JsonView extends MappingJackson2JsonView
{
    private static final Log logger = LogFactory.getLog(MyCustomHttpMessageConverter.class);

    @Override
    protected Object filterModel(Map<String, Object> model)
    {
        Object o = super.filterModel(model);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes()).getRequest();
        logger.info("MyMappingJackson2JsonView filterModel encryptData:" + request.getHeader("encryptData"));
        logger.info("MyMappingJackson2JsonView filterModel isEncrypt:" + request.getParameter("isEncrypt"));

        logger.info("filterModel Object:" + o);
//        ObjectMapper mapper = getObjectMapper();
        Map<String ,Object> map = new HashMap<>();
        map.put("code", "ok");
        map.put("msg", "in MappingJackson2JsonView");
        map.put("result", o);
        /*try {
            return mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }*/
        return map;
    }

}
